<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>计算器</title>
    <link href="../_assets/css/shared.css" rel="stylesheet" type="text/css"/>
    <link href="../_assets/css/examples.css" rel="stylesheet" type="text/css"/>
    <script src="../_assets/js/examples.js"></script>
    <script src="../lib/easeljs-NEXT.js"></script>   
    <!-- We also provide hosted minified versions of all CreateJS libraries.
      http://code.createjs.com -->
    <script src="../_assets/libs/preloadjs-NEXT.min.js"></script>

    <script id="editable">
        var update = true;
        var calculationContainer = new createjs.Container();
        var numText = "0";
        var mrNum = "";
        var opNum1 = "";
        var opNum2 = "";
        var percentNum = "";
        var op = "";
        var text = rendText(numText);
        var lastResult = "";
        var result = "";
              
        var flagPoint = true;
        var flagEqual = true;
        var flagPercent = true;
        var flagMr = true;
        var flagOp = true
        var flagRe = true;
        var flagMax = true;

        function init() {

            stage = new createjs.Stage("calculation");

            w = stage.canvas.width;
            h = stage.canvas.height;
            stage.enableMouseOver(10);
            manifest = [
                    {src: "panel.png", id: "panel"},//计算器面板
                    {src: "ac.png", id: "ac"},//清零按钮
                    {src: "0.png", id: "zero"},//数字0
                    {src: "00.png", id: "doubleZero"},//数字00
                    {src: "1.png", id: "one"},//数字1
                    {src: "2.png", id: "two"},//数字2
                    {src: "3.png", id: "three"},//数字3
                    {src: "4.png", id: "four"},//数字4
                    {src: "5.png", id: "five"},//数字5
                    {src: "6.png", id: "six"},//数字6
                    {src: "7.png", id: "seven"},//数字7
                    {src: "8.png", id: "eight"},//数字8
                    {src: "9.png", id: "nine"},//数字1
                    {src: "mc.png", id: "mc"},//mc按钮
                    {src: "mr.png", id: "mr"},//mr按钮
                    {src: "m-.png", id: "m-"},//m-按钮
                    {src: "m+.png", id: "m+"},//m+按钮
                    {src: "_.png", id: "delete"},//mc按钮
                    {src: "..png", id: "point"},//点.
                    {src: "+.png", id: "add"},//加号
                    {src: "=.png", id: "equal"},//等于号
                    {src: "-.png", id: "subt"},//减号
                    {src: "pre.png", id: "percent"},//百分号  
                    {src: "x.png", id: "mul"},//乘号
                    {src: "division.png", id: "div"},//除号        
                    {src: "sqrt.png", id: "sqrt"},//根号 
                    {src: "+-.png", id: "plusOrMinus"},//正负号

                    {src: "acd.png", id: "acd"},//清零按钮
                    {src: "0d.png", id: "zerod"},//数字0
                    {src: "00d.png", id: "doubleZerod"},//数字00
                    {src: "1d.png", id: "oned"},//数字1
                    {src: "2d.png", id: "twod"},//数字2
                    {src: "3d.png", id: "threed"},//数字3
                    {src: "4d.png", id: "fourd"},//数字4
                    {src: "5d.png", id: "fived"},//数字5
                    {src: "6d.png", id: "sixd"},//数字6
                    {src: "7d.png", id: "sevend"},//数字7
                    {src: "8d.png", id: "eightd"},//数字8
                    {src: "9d.png", id: "nined"},//数字1
                    {src: "mcd.png", id: "mcd"},//mc按钮
                    {src: "mrd.png", id: "mrd"},//mr按钮
                    {src: "m-d.png", id: "m-d"},//m-按钮
                    {src: "m+d.png", id: "m+d"},//m+按钮
                    {src: "_d.png", id: "deleted"},//mc按钮
                    {src: ".d.png", id: "pointd"},//点.
                    {src: "+d.png", id: "addd"},//加号
                    {src: "=d.png", id: "equald"},//等于号
                    {src: "-d.png", id: "subtd"},//减号
                    {src: "pred.png", id: "percentd"},//百分号  
                    {src: "xd.png", id: "muld"},//乘号
                    {src: "divisiond.png", id: "divd"},//除号        
                    {src: "sqrtd.png", id: "sqrtd"},//根号 
                    {src: "+-d.png", id: "plusOrMinusd"},//正负号
            ];


            loader = new createjs.LoadQueue(false);
            loader.addEventListener("complete", handleComplete);
            loader.loadManifest(manifest, true, "calculation_images/");
        }

        function handleComplete() {
            panelImg = loader.getResult("panel");
            acImg = loader.getResult("ac");
            zeroImg = loader.getResult("zero");
            doubleZeroImg = loader.getResult("doubleZero");
            
            oneImg = loader.getResult("one");
            twoImg = loader.getResult("two");
            threeImg = loader.getResult("three");
            fourImg = loader.getResult("four");
            fiveImg = loader.getResult("five");
            sixImg = loader.getResult("six");
            sevenImg = loader.getResult("seven");
            eightImg = loader.getResult("eight");
            nineImg = loader.getResult("nine");

            pointImg = loader.getResult("point");
            addImg = loader.getResult("add");
            subtImg = loader.getResult("subt");
            divImg = loader.getResult("div");
            mulImg = loader.getResult("mul");
            equalImg = loader.getResult("equal");
            perImg = loader.getResult("percent");
            pmImg = loader.getResult("plusOrMinus");
            sqrtImg = loader.getResult("sqrt");
            mcImg = loader.getResult("mc");
            mrImg = loader.getResult("mr");
            msubtImg = loader.getResult("m-");
            maddImg = loader.getResult("m+");
            deleteImg = loader.getResult("delete");

            //
            acImgd = loader.getResult("acd");
            zeroImgd = loader.getResult("zerod");
            doubleZeroImgd = loader.getResult("doubleZerod");
            
            oneImgd = loader.getResult("oned");
            twoImgd = loader.getResult("twod");
            threeImgd = loader.getResult("threed");
            fourImgd = loader.getResult("fourd");
            fiveImgd = loader.getResult("fived");
            sixImgd = loader.getResult("sixd");
            sevenImgd = loader.getResult("sevend");
            eightImgd = loader.getResult("eightd");
            nineImgd = loader.getResult("nined");

            pointImgd = loader.getResult("pointd");
            addImgd = loader.getResult("addd");
            subtImgd = loader.getResult("subtd");
            divImgd = loader.getResult("divd");
            mulImgd = loader.getResult("muld");
            equalImgd = loader.getResult("equald");
            perImgd = loader.getResult("percentd");
            pmImgd = loader.getResult("plusOrMinusd");
            sqrtImgd = loader.getResult("sqrtd");
            mcImgd = loader.getResult("mcd");
            mrImgd = loader.getResult("mrd");
            msubtImgd = loader.getResult("m-d");
            maddImgd = loader.getResult("m+d");
            deleteImgd = loader.getResult("deleted");
            

            var panel = rendPanel(panelImg,100,100,panelImg.width,panelImg.height);

            function rendacBut(img,x,y,width,height){
                //计算器按钮
                var acBut = new createjs.Shape();
                acBut.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                acBut.x = x;
                acBut.y = y;
                acBut.cursor = "pointer";

                acBut.addEventListener("mousedown",function(){
                    var child = stage.children[0];
                    var cLength = child.numChildren;

                    if(!flagRe){
                        child.removeChildAt(cLength-1);
                        flagRe = true;
                    }
                    numText = "0";
                    op ="";
                    opNum1 = "";
                    opNum2 = "";
                    result = "";
                    text.text = numText;
                    flagPoint = true;
                    flagPm = true;

                    var child = stage.children[0];
                    child.removeChildAt(2);
                    var acBut = rendacBut(acImgd,x,y,width,height);
                    calculationContainer.addChildAt(acBut,2);


                    // var myGraphics = new createjs.Graphics().beginBitmapFill(acImgd);
                    // acBut = acBut.set({graphics:myGraphics});
                    calculationContainer.updateCache();
                    update = true;
                });

                acBut.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(2);
                    var acBut = rendacBut(acImg,x,y,width,height);
                    calculationContainer.addChildAt(acBut,2);
                    calculationContainer.updateCache();
                    update = true;
                });                

                return acBut;
            }


            //清零按钮
            var acBut = rendacBut(acImg,195+zeroImg.width*3 + addImg.width,215,acImg.width,acImg.height);


            function rendZeroButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_num("0");
                    var child = stage.children[0];
                    child.removeChildAt(12);
                    var button = rendZeroButton(zeroImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,12);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(12);
                    var button = rendZeroButton(zeroImg,x,y,width,height);
                    calculationContainer.addChildAt(button,12);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }

            //数字0按钮
            var zeroBut = rendZeroButton(zeroImg,145,425,zeroImg.width,zeroImg.height);

            function rendDoubleZeroButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    if(Number(numText) == 0 && !(isPoint(numText).bool) && flagEqual){
                        reRendtext_num("0");
                    }
                    else{
                        if(!flagEqual){
                            reRendtext_num("0");
                        }
                        else{
                            reRendtext_num("00");
                        }
                        
                    }
                    var child = stage.children[0];
                    child.removeChildAt(13);
                    var button = rendDoubleZeroButton(doubleZeroImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,13);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(13);
                    var button = rendDoubleZeroButton(doubleZeroImg,x,y,width,height);
                    calculationContainer.addChildAt(button,13);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }

            var doubleZeroBut = rendDoubleZeroButton(doubleZeroImg,155+zeroImg.width,425,doubleZeroImg.width,doubleZeroImg.height);

             function rendOneButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_num("1");

                    var child = stage.children[0];
                    child.removeChildAt(14);
                    var button = rendOneButton(oneImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,14);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(14);
                    var button = rendOneButton(oneImg,x,y,width,height);
                    calculationContainer.addChildAt(button,14);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }

            //数字1
            var oneBut = rendOneButton(oneImg,145,415 - zeroImg.height,oneImg.width,oneImg.height);

            function rendTwoButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_num("2");

                    var child = stage.children[0];
                    child.removeChildAt(15);
                    var button = rendTwoButton(twoImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,15);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(15);
                    var button = rendTwoButton(twoImg,x,y,width,height);
                    calculationContainer.addChildAt(button,15);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }

            var twoBut = rendTwoButton(twoImg,155+oneImg.width,415 - zeroImg.height,twoImg.width,twoImg.height);


            function rendThreeButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_num("3");

                    var child = stage.children[0];
                    child.removeChildAt(16);
                    var button = rendThreeButton(threeImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,16);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(16);
                    var button = rendThreeButton(threeImg,x,y,width,height);
                    calculationContainer.addChildAt(button,16);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var threeBut = rendThreeButton(threeImg,165+oneImg.width*2,415 - zeroImg.height,threeImg.width,threeImg.height);

            function rendFourButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_num("4");

                    var child = stage.children[0];
                    child.removeChildAt(17);
                    var button = rendFourButton(fourImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,17);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(17);
                    var button = rendFourButton(fourImg,x,y,width,height);
                    calculationContainer.addChildAt(button,17);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var fourBut = rendFourButton(fourImg,145,405 - zeroImg.height*2,fourImg.width,fourImg.height);

            function rendFiveButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_num("5");

                    var child = stage.children[0];
                    child.removeChildAt(18);
                    var button = rendFiveButton(fiveImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,18);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(18);
                    var button = rendFiveButton(fiveImg,x,y,width,height);
                    calculationContainer.addChildAt(button,18);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }

            var fiveBut = rendFiveButton(fiveImg,155+fourImg.width,405 - zeroImg.height*2,fiveImg.width,fiveImg.height);

            function rendSixButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_num("6");

                    var child = stage.children[0];
                    child.removeChildAt(19);
                    var button = rendSixButton(sixImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,19);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(19);
                    var button = rendSixButton(sixImg,x,y,width,height);
                    calculationContainer.addChildAt(button,19);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var sixBut = rendSixButton(sixImg,165+fourImg.width*2,405 - sixImg.height*2,sixImg.width,fiveImg.height);


            function rendSevenButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_num("7");

                    var child = stage.children[0];
                    child.removeChildAt(20);
                    var button = rendSevenButton(sevenImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,20);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(20);
                    var button = rendSevenButton(sevenImg,x,y,width,height);
                    calculationContainer.addChildAt(button,20);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var sevenBut = rendSevenButton(sevenImg,145,395 - zeroImg.height*3,sevenImg.width,sevenImg.height);

            function rendEightButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_num("8");

                    var child = stage.children[0];
                    child.removeChildAt(21);
                    var button = rendEightButton(eightImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,21);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(21);
                    var button = rendEightButton(eightImg,x,y,width,height);
                    calculationContainer.addChildAt(button,21);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var eightBut = rendEightButton(eightImg,155+sevenImg.width,395 - eightImg.height*3,eightImg.width,sevenImg.height);


            function rendNineButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_num("9");

                    var child = stage.children[0];
                    child.removeChildAt(22);
                    var button = rendNineButton(nineImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,22);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(22);
                    var button = rendNineButton(nineImg,x,y,width,height);
                    calculationContainer.addChildAt(button,22);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var ninetBut = rendNineButton(nineImg,165+sevenImg.width*2,395 - nineImg.height*3,nineImg.width,sevenImg.height);


            function rendPointButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    if(flagPoint && Number(numText) == 0 && flagEqual && flagMr){
                        numText = "0."
                        text.text = numText;
                        calculationContainer.updateCache();
                        update = true;
                        flagPoint = false;
                    }
                    else if(flagPoint && numText != "0" && flagEqual && flagMr){
                        numText = numText+".";
                        text.text = numText;
                        flagPoint = false;
                        calculationContainer.updateCache();
                        update = true;
                    }
                    else if(!flagEqual && flagMr){
                        numText = "0."
                        text.text = numText;
                        calculationContainer.updateCache();
                        update = true;
                        flagPoint = false;
                        flagEqual = true;
                    }
                    else if(!flagEqual && !flagMr){
                        numText = "0."
                        text.text = numText;
                        calculationContainer.updateCache();
                        update = true;
                        flagPoint = false;
                        flagMr = true;
                        flagEqual = true;
                    }
                    else if(flagEqual && !flagMr){
                        numText = "0."
                        text.text = numText;
                        calculationContainer.updateCache();
                        update = true;
                        flagPoint = false;
                        flagMr = true;
                    }
                    var child = stage.children[0];
                    child.removeChildAt(3);
                    var button = rendPointButton(pointImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,3);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(3);
                    var button = rendPointButton(pointImg,x,y,width,height);
                    calculationContainer.addChildAt(button,3);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var pointBut = rendPointButton(pointImg,165+zeroImg.width*2,425,pointImg.width,pointImg.height);



            function rendAddButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_op1("+");
                    var child = stage.children[0];
                    child.removeChildAt(4);
                    var button = rendAddButton(addImgd,x,y,addImgd.width,addImgd.height);
                    calculationContainer.addChildAt(button,4);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(4);
                    var button = rendAddButton(addImg,x,y,width,height);
                    calculationContainer.addChildAt(button,4);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var addtBut = rendAddButton(addImg,185+zeroImg.width*3,390,addImg.width,addImg.height);


            function rendSubButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_op1("-");
                    var child = stage.children[0];
                    child.removeChildAt(5);
                    var button = rendSubButton(subtImgd,x,y,subtImgd.width,subtImgd.height);
                    calculationContainer.addChildAt(button,5);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(5);
                    var button = rendSubButton(subtImg,x,y,width,height);
                    calculationContainer.addChildAt(button,5);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var subtBut = rendSubButton(subtImg,185+zeroImg.width*3,380-subtImg.height,subtImg.width,subtImg.height);

            function rendMulButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_op1("*");
                    var child = stage.children[0];
                    child.removeChildAt(6);
                    var button = rendMulButton(mulImgd,x,y,subtImgd.width,subtImgd.height);
                    calculationContainer.addChildAt(button,6);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(6);
                    var button = rendMulButton(mulImg,x,y,width,height);
                    calculationContainer.addChildAt(button,6);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var mulBut = rendMulButton(mulImg,195+zeroImg.width*3 + addImg.width,380-subtImg.height,mulImg.width,mulImg.height);


            function rendPerButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    if(opNum1 !="" && op !="" &&flagEqual){
                        opNum1 = opNum1.toString();
                        opNum2 = opNum2.toString();
                        var op1Length = opNum1.length;
                        var op2Length = opNum2.length;
                        
                        var point1 = isPoint(opNum1);
                        var point2 = isPoint(opNum2);
                        var pLength1 = op1Length - point1.location - 1;
                        var pLength2 = op2Length - point2.location - 1;

                        if(pLength1 >= pLength2 && op != "*"){
                            var length = pLength1;
                        }
                        else if(pLength1 <= pLength2 && op != "*"){
                            var length = pLength2;
                        }
                        else if(pLength1 >= pLength2 && op == "*"){
                            var length = pLength1+pLength2;
                        }
                        else if(pLength1 <= pLength2 && op == "*"){
                            var length = pLength1+pLength2;
                        }
                        console.log(length)
                        if(length == 0){
                            length =2;
                        }
                        
                        switch(op){
                            case "+": if(flagPercent){
                                          opNum2 = numText;
                                          opNum2 = (opNum2/100 * opNum1).toFixed(length);
                                          flagPercent = false;
                                      }
                                      numText = Number(opNum1) + Number(opNum2);
                                      
                                      break;
                            case "-": if(flagPercent){
                                          opNum2 = numText;
                                          opNum2 = opNum2/100 * opNum1;
                                          flagPercent = false;
                                      }
                                      numText = Number(opNum1) - Number(opNum2);
                                      break; 
                            case "*": if(flagPercent){
                                          opNum2 = numText;
                                          opNum2 = opNum2/100;
                                          flagPercent = false;
                                      }
                                      numText = Number(opNum1) * Number(opNum2);
                                      
                                      break;
                            case "/": if(flagPercent){
                                          opNum2 = numText;
                                          opNum2 = opNum2/100;
                                          flagPercent = false;
                                      }
                                      numText = Number(opNum1) / Number(opNum2);
                                      
                                      break;
                        }

                        console.log(opNum1,opNum2,op,numText)

                        opNum1 = opNum1.toString();
                        opNum2 = opNum2.toString();
                        var op1Length = opNum1.length;
                        var op2Length = opNum2.length;
                        
                        var point1 = isPoint(opNum1);
                        var point2 = isPoint(opNum2);
                        var pLength1 = op1Length - point1.location - 1;
                        var pLength2 = op2Length - point2.location - 1;

                        if(pLength1 >= pLength2 && op != "*"){
                            var length = pLength1;
                        }
                        else if(pLength1 <= pLength2 && op != "*"){
                            var length = pLength2;
                        }
                        else if(pLength1 >= pLength2 && op == "*"){
                            var length = pLength1+pLength2;
                        }
                        else if(pLength1 <= pLength2 && op == "*"){
                            var length = pLength1+pLength2;
                        }


                        numText = Number(numText);

                        if(!isE(numText)){
                            if(numText<=999999999 || numText == "Infinity"){
                                if(op != "/"){
                                    numText = numText.toFixed(length);
                                }
                                console.log(length)
                                numText = numText.toString();
                                if(numText.length>9 && (isPoint(numText).bool) ){
                                    numText = numText.slice(0,10);
                                    if(numText.slice(9,10)=="."){
                                        text.font="bold 50px Arial";
                                        numText = numText.slice(0,9);
                                    }      
                                    else{
                                        text.font="bold 48px Arial";
                                    }                 
                                }
                                else{
                                
                                    numText = numText.slice(0,9);
                                    text.font = "bold 50px Arial";

                                    
                                }
                                
                                if(op == "/" && opNum2 == 0){
                                    numText = 0;
                                    if(flagRe){
                                        var eText = reRendtext_e();
                                        calculationContainer.addChild(eText);
                                        flagRe = false;
                                    }
                                }

                            }
                            else{
                                result = numText;

                                result = result.toString();
                                if(result.length>9 && (isPoint(result).bool) ){
                                    result = result.slice(0,10);
                                    if(result.slice(9,10)=="."){
                                        text.font="bold 50px Arial";
                                        result = result.slice(0,9);
                                    }      
                                    else{
                                        text.font="bold 48px Arial";
                                    }                 
                                }
                                else{
                                
                                    result = result.slice(0,9);
                                    text.font = "bold 50px Arial";


                                    
                                }
                                numText = lastResult;

                            }
                        }
                        else{
                            numText ="0";
                        }

                        //console.log(opNum1,opNum2,op,result)
                        lastResult = numText;
                        if(result != ""){
                            text.font="bold 48px Arial";
                            text.text = result;
                            if(flagRe){
                                var eText = reRendtext_e();
                                calculationContainer.addChild(eText);
                                flagRe = false;
                            }
                        }
                        else{
                            text.text = numText;
                        }

                        calculationContainer.updateCache();
                        update = true;

                        flagPoint = true;
                        flagEqual = false;
                        opNum1 = "";
                        opNum2 = "";
                    }



                    var child = stage.children[0];
                    child.removeChildAt(7);
                    var button = rendPerButton(perImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,7);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(7);
                    var button = rendPerButton(perImg,x,y,width,height);
                    calculationContainer.addChildAt(button,7);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var perBut = rendPerButton(perImg,185+zeroImg.width*3,370-subtImg.height*2,perImg.width,perImg.height);

            function rendDivButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    reRendtext_op1("/");
                    var child = stage.children[0];
                    child.removeChildAt(10);
                    var button = rendDivButton(divImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,10);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(10);
                    var button = rendDivButton(divImg,x,y,width,height);
                    calculationContainer.addChildAt(button,10);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var divBut = rendDivButton(divImg,195+zeroImg.width*3 + addImg.width,370-subtImg.height*2,divImg.width,divImg.height);

            function rendPmButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    if(numText >0 && (numText.toString()).length < 9 && !(isPoint(numText)).bool && numText != ""){
                        numText = "-" + numText;
                        text.text = numText;
                    }
                    else if(numText >0 && (numText.toString()).length < 10 && (isPoint(numText)).bool && numText != ""){
                        numText = "-" + numText;
                        text.text = numText;
                    }
                    else if(numText < 0 && numText != ""){
                        numText = numText.toString();
                        numText = numText.slice(1,numText.length);
                        text.text = numText;
                    }
                    else if(numText == ""){
                        if(opNum1 >0 && (opNum1.toString()).length < 9 && !(isPoint(opNum1)).bool && opNum1 != ""){
                        opNum1 = "-" + opNum1;
                        text.text = opNum1;
                        }
                        else if(opNum1 >0 && (opNum1.toString()).length < 10 && (isPoint(opNum1)).bool && opNum1 != ""){
                            opNum1 = "-" + opNum1;
                            text.text = opNum1;
                        }
                        else if(opNum1 < 0 && opNum1 != ""){
                            opNum1 = opNum1.toString();
                            opNum1 = opNum1.slice(1,opNum1.length);
                            text.text = opNum1;
                        }
                    }
                        
                    
                    calculationContainer.updateCache();
                    update = true;
                    var child = stage.children[0];
                    child.removeChildAt(9);
                    var button = rendPmButton(pmImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,9);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(9);
                    var button = rendPmButton(pmImg,x,y,width,height);
                    calculationContainer.addChildAt(button,9);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var pmBut = rendPmButton(pmImg,185+zeroImg.width*3,395 - nineImg.height*3,pmImg.width,pmImg.height);

            function rendSqrtButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    numText = Math.sqrt(numText);
                    numText = numText.toString();

                    if(numText.length>9 && (isPoint(numText).bool) ){
                        numText = numText.slice(0,10);
                        if(numText.slice(9,10)=="."){
                            text.font="bold 50px Arial";
                            numText = numText.slice(0,9);
                        }
                        else{
                            text.font="bold 48px Arial";
                        }
                    }
                    else{
                        numText = numText.slice(0,9)
                    }
                    
                    text.text = numText;
                    calculationContainer.updateCache();
                    update = true;
                    flagPoint = true;
                    flagEqual = false;
                    opNum1 = "";
                    opNum2 = "";
                    var child = stage.children[0];
                    child.removeChildAt(11);
                    var button = rendSqrtButton(sqrtImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,11);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(11);
                    var button = rendSqrtButton(sqrtImg,x,y,width,height);
                    calculationContainer.addChildAt(button,11);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var sqrtBut = rendSqrtButton(sqrtImg,195+zeroImg.width*3 + addImg.width,395 - nineImg.height*3,sqrtImg.width,sqrtImg.height);


            function rendEqualButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    if(flagEqual){
                        if(numText != ""){
                            opNum2 = numText;
                        }
                        else{
                            opNum2 = lastResult;
                        }
                    }
                    
                    if(opNum1 !="" && opNum2 != ""){
                        switch(op){
                            case "+": numText = Number(opNum1) + Number(opNum2);
                                      break;
                            case "-": numText = Number(opNum1) - Number(opNum2);
                                      break; 
                            case "*": numText = Number(opNum1) * Number(opNum2);
                                      break;
                            case "/": numText = Number(opNum1) / Number(opNum2);
                                      break;
                        }

                        opNum1 = opNum1.toString();
                        opNum2 = opNum2.toString();
                        var op1Length = opNum1.length;
                        var op2Length = opNum2.length;
                        
                        var point1 = isPoint(opNum1);
                        var point2 = isPoint(opNum2);
                        var pLength1 = op1Length - point1.location - 1;
                        var pLength2 = op2Length - point2.location - 1;

                        if(pLength1 >= pLength2 && op != "*"){
                            var length = pLength1;
                        }
                        else if(pLength1 <= pLength2 && op != "*"){
                            var length = pLength2;
                        }
                        else if(pLength1 >= pLength2 && op == "*"){
                            var length = pLength1+pLength2;
                        }
                        else if(pLength1 <= pLength2 && op == "*"){
                            var length = pLength1+pLength2;
                        }


                        numText = Number(numText);

                        if(!isE(numText)){
                            if(numText<=999999999 || numText == "Infinity"){
                                if(op != "/"){
                                    numText = numText.toFixed(length);
                                }

                                numText = numText.toString();
                                if(numText.length>9 && (isPoint(numText).bool) ){
                                    numText = numText.slice(0,10);
                                    if(numText.slice(9,10)=="."){
                                        text.font="bold 50px Arial";
                                        numText = numText.slice(0,9);
                                    }      
                                    else{
                                        text.font="bold 48px Arial";
                                    }                 
                                }
                                else{
                                
                                    numText = numText.slice(0,9);
                                    text.font = "bold 50px Arial";
                                    
                                }
                                
                                if(op == "/" && opNum2 == 0){
                                    numText = 0;
                                    if(flagRe){
                                        var eText = reRendtext_e();
                                        calculationContainer.addChild(eText);
                                        flagRe = false;
                                    }
                                }

                            }
                            else{
                                result = numText;

                                result = result.toString();
                                if(result.length>9 && (isPoint(result).bool) ){
                                    result = result.slice(0,10);
                                    if(result.slice(9,10)=="."){
                                        text.font="bold 50px Arial";
                                        result = result.slice(0,9);
                                    }      
                                    else{
                                        text.font="bold 48px Arial";
                                    }                 
                                }
                                else{
                                
                                    result = result.slice(0,9);
                                    text.font = "bold 50px Arial";
                                    
                                }
                                numText = lastResult;

                            }
                        }
                        else{
                            numText ="0";
                        }

                        //console.log(opNum1,opNum2,op,result)
                        lastResult = numText;
                        if(result != ""){
                            text.font="bold 48px Arial";
                            text.text = result;
                            if(flagRe){
                                var eText = reRendtext_e();
                                calculationContainer.addChild(eText);
                                flagRe = false;
                            }
                        }
                        else{
                            text.text = numText;
                        }
                        
                        calculationContainer.updateCache();
                        update = true;
                        flagPoint = true;
                        flagEqual = false;
                        
                        opNum1 = numText;
                        //opNum2 = "";
                        result = "";
                    }


                    var child = stage.children[0];
                    child.removeChildAt(8);
                    var button = rendEqualButton(equalImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,8);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(8);
                    var button = rendEqualButton(equalImg,x,y,width,height);
                    calculationContainer.addChildAt(button,8);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var equalBut = rendEqualButton(equalImg,195+zeroImg.width*3 + addImg.width,390,equalImg.width,equalImg.height);

            function rendMcButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    mrNum = 0;
                    var child = stage.children[0];
                    child.removeChildAt(23);
                    var button = rendMcButton(mcImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,23);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(23);
                    var button = rendMcButton(mcImg,x,y,width,height);
                    calculationContainer.addChildAt(button,23);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var mcBut = rendMcButton(mcImg,145,398 - zeroImg.height*4,mcImg.width,mcImg.height);

            function rendMrButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    mrNum = mrNum.toString();
                    if(mrNum.length>9 && (isPoint(mrNum).bool) ){
                        mrNum = mrNum.slice(0,10);
                        if(mrNum.slice(9,10)=="."){
                            text.font="bold 50px Arial";
                            mrNum = mrNum.slice(0,9);
                            text.text = mrNum;
                        }
                    }
                    else{
                        mrNum = mrNum.slice(0,9)
                    }

                    numText = mrNum;
                    text.text = numText;
                    calculationContainer.updateCache();
                    update = true;
                    flagMr = false;
                    flagPoint = true;
                    var child = stage.children[0];
                    child.removeChildAt(24);
                    var button = rendMrButton(mrImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,24);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(24);
                    var button = rendMrButton(mrImg,x,y,width,height);
                    calculationContainer.addChildAt(button,24);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var mrBut = rendMrButton(mrImg,155+sevenImg.width,398 - zeroImg.height*4,mrImg.width,mrImg.height);

            function rendMsubButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    numText = numText.toString();
                    mrNum = mrNum.toString();
                    var mLength = mrNum.length;
                    var nLength = numText.length;

                    var point1 = isPoint(numText);
                    var point2 = isPoint(mrNum);
                    var pLength1 = nLength - point1.location - 1;
                    var pLength2 = mLength - point2.location - 1;

                    if(pLength1 > pLength2){
                        var length = pLength1;
                    }
                    else{
                        var length = pLength2;
                    }

                    mrNum = Number(mrNum);
                    numText = Number(numText);
                    mrNum -= numText;
                    if(length > 0){
                        mrNum = mrNum.toFixed(length);
                    }

                    flagMr = false;

                    var child = stage.children[0];
                    child.removeChildAt(25);
                    var button = rendMsubButton(msubtImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,25);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(25);
                    var button = rendMsubButton(msubtImg,x,y,width,height);
                    calculationContainer.addChildAt(button,25);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var msubtBut = rendMsubButton(msubtImg,165+sevenImg.width*2,398 - zeroImg.height*4,msubtImg.width,msubtImg.height);


            function rendMaddButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    numText = numText.toString();
                    mrNum = mrNum.toString();
                    var mLength = mrNum.length;
                    var nLength = numText.length;

                    var point1 = isPoint(numText);
                    var point2 = isPoint(mrNum);
                    var pLength1 = nLength - point1.location - 1;
                    var pLength2 = mLength - point2.location - 1;

                    if(pLength1 > pLength2){
                        var length = pLength1;
                    }
                    else{
                        var length = pLength2;
                    }

                    mrNum = Number(mrNum);
                    numText = Number(numText);
                    mrNum += numText;
                    if(length > 0){
                        mrNum = mrNum.toFixed(length);
                    }


                    flagMr = false;

                    var child = stage.children[0];
                    child.removeChildAt(26);
                    var button = rendMaddButton(maddImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,26);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(26);
                    var button = rendMaddButton(maddImg,x,y,width,height);
                    calculationContainer.addChildAt(button,26);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var maddBut = rendMaddButton(maddImg,185+sevenImg.width*3,398 - zeroImg.height*4,maddImg.width,maddImg.height);

            function rendDelButton(img,x,y,width,height){
                var button = new createjs.Shape();
                button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
                button.x = x;
                button.y = y;
                button.cursor = "pointer";

                button.addEventListener("mousedown",function(){
                    numText = numText.toString();
                    var length = numText.length;
                    var finalchar = numText.slice(length-1,length);
                    if(finalchar == "."){
                        flagPoint = true;
                    }
                    numText = numText.slice(0,length-1);
                    if(numText =="" || numText == "-"){
                        numText = "0";
                        text.text = numText;
                    }

                    else{
                        text.text = numText;
                    }
                    
                    calculationContainer.updateCache();
                    update = true;

                    var child = stage.children[0];
                    child.removeChildAt(27);
                    var button = rendDelButton(deleteImgd,x,y,width,height);
                    calculationContainer.addChildAt(button,27);


                    calculationContainer.updateCache();
                    update = true;
                });

                button.addEventListener("pressup",function(){
                    var child = stage.children[0];
                    child.removeChildAt(27);
                    var button = rendDelButton(deleteImg,x,y,width,height);
                    calculationContainer.addChildAt(button,27);
                    calculationContainer.updateCache();
                    update = true;
                }); 

                return button;
            }
            var deleteBut = rendDelButton(deleteImg,195+sevenImg.width*3 + maddImg.width,398 - zeroImg.height*4,deleteImg.width,deleteImg.height);


            

            calculationContainer.addChild(panel,text,acBut,pointBut,
                addtBut,subtBut,mulBut,perBut,equalBut,pmBut,divBut,sqrtBut,
                zeroBut,doubleZeroBut,oneBut,twoBut,threeBut,fourBut,fiveBut,sixBut,sevenBut,eightBut,ninetBut,
                mcBut,mrBut,msubtBut,maddBut,deleteBut);
            calculationContainer.cache(0,0,w,h);

            createjs.Touch.enable(stage);
            createjs.Ticker.addEventListener("tick", tick);
            stage.addChild(calculationContainer);
        }

        function tick(event) {
            // this set makes it so the stage only re-renders when an event handler indicates a change has happened.
            if (update) {
                update = false; // only update once
                stage.update(event);
            }
        }
  

        function rendPanel(img,x,y,width,height){
            //计算器面板
            var panel = new createjs.Shape();
            panel.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
            panel.x = x;
            panel.y = y;
            //panel.cursor = "pointer";
            panel.addEventListener("mousedown",function(event){
                calculationContainer.offset = {x:event.stageX - calculationContainer.x,y:event.stageY - calculationContainer.y}
            });

            panel.addEventListener("pressmove",function(event){
                calculationContainer.x = event.stageX - calculationContainer.offset.x;
                calculationContainer.y = event.stageY - calculationContainer.offset.y;

                update = true;
            });
            
            return panel;
        }

        

        function rendText(text){
            //计算结果
            var numText = new createjs.Text(text, "bold 50px Arial", "black");
            
            var y = 130;
            
            numText.x = 420;
            numText.y = 130;
            numText.textAlign = "right";
            
            return numText;
        }

        function rendButton(img,x,y,width,height){
            //计算器按钮
            var button = new createjs.Shape();
            button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
            button.x = x;
            button.y = y;
            button.cursor = "pointer";



            return button;
        }

        function reRendtext_num(num){
            var child = stage.children[0];
            var cLength = child.numChildren;

            if(!flagRe){
                child.removeChildAt(cLength-1);

                flagRe = true;
            }

            if((Number(numText) == 0 && flagPoint) || (!flagEqual && flagPoint) ||(!flagMr)){
                numText = num;
                if(!flagEqual){
                    opnum2 = "";
                }
                flagEqual = true;
                flagMr = true;
            }
            else{
                text.font = "bold 50px Arial"
                numText = numText+num;
            }

            var i = numText.length-1;
            if(i >8){
                var point = isPoint(numText);
                if(point.bool){
                    text.font = "bold 48px Arial"
                    numText = numText.slice(0,10);
                }
                else{
                    text.font = "bold 50px Arial"
                    numText = numText.slice(0,9);
                }
            }

            text.text = numText;
            calculationContainer.updateCache();
            update = true;
        }

        function reRendtext_op1(operation){
            flagPoint = true;
            flagPercent =true;
            //if(flagEqual){
                if(op != operation && opNum1 == ""){
                    op = operation;
                    if(numText !=""){
                        opNum1 = numText;
                    }
                    numText = "";
                }

                //opNum2 = "";
                if(opNum1 != "" && opNum2 ==""){
                    opNum2 = numText;
                    switch(op){
                        case "+": if(opNum2 ==""){
                                      opNum2 = 0;
                                      numText = Number(opNum1) + 0;
                                  }
                                  else{
                                      numText = Number(opNum1) + Number(opNum2);
                                  }
                                  
                                  break;
                        case "-": if(opNum2 ==""){
                                      opNum2 = 0;
                                      numText = Number(opNum1) - 0;
                                  }
                                  else{
                                      numText = Number(opNum1) - Number(opNum2);
                                  }
                                  
                                  break; 
                        case "*": if(opNum2 ==""){
                                      opNum2 = 1;
                                      numText = Number(opNum1) * 1;
                                  }
                                  else{
                                      numText = Number(opNum1) * Number(opNum2);
                                  }
                                  break;
                        case "/": if(opNum2 ==""){
                                      opNum2 = 1
                                      numText = Number(opNum1) / 1;
                                  }
                                  else{
                                      numText = Number(opNum1) / Number(opNum2);
                                  }

                                  break;
                    }

                    opNum1 = opNum1.toString();
                    opNum2 = opNum2.toString();
                    var op1Length = opNum1.length;
                    var op2Length = opNum2.length;
                    
                    var point1 = isPoint(opNum1);
                    var point2 = isPoint(opNum2);
                    var pLength1 = op1Length - point1.location - 1;
                    var pLength2 = op2Length - point2.location - 1;

                    if(pLength1 >= pLength2 && op != "*"){
                        var length = pLength1;
                    }
                    else if(pLength1 <= pLength2 && op != "*"){
                        var length = pLength2;
                    }
                    else if(pLength1 >= pLength2 && op == "*"){
                        var length = pLength1+pLength2;
                    }
                    else if(pLength1 <= pLength2 && op == "*"){
                        var length = pLength1+pLength2;
                    }


                    numText = Number(numText);
                    if(!isE(numText)){
                        if(numText<=999999999 || numText == "Infinity"){
                            if(op != "/"){
                                numText = numText.toFixed(length);
                            }

                            numText = numText.toString();
                            if(numText.length>9 && (isPoint(numText).bool) ){
                                numText = numText.slice(0,10);
                                if(numText.slice(9,10)=="."){
                                    text.font="bold 50px Arial";
                                    numText = numText.slice(0,9);
                                }      
                                else{
                                    text.font="bold 48px Arial";
                                }                 
                            }
                            else{
                                numText = numText.slice(0,9);
                                text.font = "bold 50px Arial";
                            }

                            if(op == "/" && opNum2 == 0){
                                numText = 0;
                                if(flagRe){
                                    var eText = reRendtext_e();
                                    calculationContainer.addChild(eText);
                                    flagRe = false;
                                }
                            }
                        }
                        else{
                            result = numText;
                            result = result.toString();
                            if(result.length>9 && (isPoint(result).bool) ){
                                result = result.slice(0,10);
                                if(result.slice(9,10)=="."){
                                    text.font="bold 50px Arial";
                                    result = result.slice(0,9);
                                }      
                                else{
                                    text.font="bold 48px Arial";
                                }                 
                            }
                            else{
                            
                                result = result.slice(0,9);
                                text.font = "bold 50px Arial";      
                            }
                            numText = lastResult;
                        }
                    }

                    else{
                        numText ="0";
                        opNum1 = "";
                        opNum2 = "";
                    }
                    
                    lastResult = numText;
                    if(result != ""){
                        text.font="bold 48px Arial";
                        text.text = result;
                        if(flagRe){
                            var eText = reRendtext_e();
                            calculationContainer.addChild(eText);
                            flagRe = false;
                        }
                    }
                    else{
                        text.text = numText;
                    }
                    calculationContainer.updateCache();
                    update = true;
                    opNum1 = numText;
                    lastResult = numText;
                    numText = "";
                    opNum2 = "";
                }
                else{
                    opNum1 = numText;
                    opNum2 = "";
                    numText = "";
                }

                if(op != operation && opNum1 !=""){
                    op = operation;
                    numText = "";
                }     
            
        }

        function reRendtext_e(){
            var eText = new createjs.Text("E", "bold 25px Arial", "black");
            eText.name = "e";                     
            eText.x = 160;
            eText.y = 155;
            flagRe = false;
            flagEqual = true;
            return eText;
        }

        

        function isPoint(str){
            str = str.toString();
            var point={bool:false,location:0};

            for( var i = 0;i < str.length;i++){
                if(str[i] == "."){
                    point.bool = true;
                    point.location = i;
                }
            }
            if(point.location == 0){
                point.location = str.length-1;
            }
            return point;
        }

        function isE(str){
            str = str.toString();
            var bool=false;

            for( var i = 0;i < str.length;i++){
                if(str[i] == "e"){
                    bool = true;
                }
            }
            return bool;
        }


    </script>
</head>
<body onload="init();">
    <div>
        <canvas id="calculation" width="900" height="800" style="background: white"></canvas>
    </div>
</body>
</html>